/**
 * Created by Administrator on 2017/10/10.
 */
angular.module('module.set')
    .controller('AutoStartController', AutoStartController);
AutoStartController.$inject = ['$state', 'SetService', '$localStorage'];
function AutoStartController($state, SetService, $localStorage) {

    var vm = this;
    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }
    var shopid = $localStorage[environmental + "shopid"];

    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    vm.save = save; // 保存
    vm.alertTimeByTimeimgID = alertTimeByTimeimgID;
    vm.addTime = addTime;
    vm.openmotal = openmotal; // 强制开机弹出模态框
    vm.closemotal = closemotal; // 强制关机弹出模态框

    // 获取开关机时间
    SetService.getTime(shopid, function (data) {
        vm.time = data;
        time(data, 0);
        console.log(data);
    });

    function time(data, i) {
        if (i < data.length) {
            var item = data[i];
            item.openTime = timeFilter(item.open).text;
            item.closeTime = timeFilter(item.close).text;
            if (item.type == 1) {
                item.type = true;
            } else {
                item.type = false;
            }
            time(data, i + 1);
        }
    }

    function save(data) {
        if (data.type) {
            data.type = 1;
        }
        else {
            data.type = 0;
        }
        SetService.alertTime(data, function (info) {
            // 注释
            (info.type == 1) ? data.type = true : data.type = false;
        }, function () {
            data.type = !data.type;
        })
    }

    function alertTimeByTimeimgID(timeingid, open, close) {
        $state.go("setTime", {timeingid: timeingid, open: open, close: close});
    }

    function addTime() {
        $state.go('setTime');
    }

    // 强制开机弹出模态框
    function openmotal() {
        $('#exampleModal').modal('show');
        vm.status = 1;

        // 一键开机
        vm.sure = function () {
            vm.open = true;
            vm.alarm = false;
            vm.close = false;
            $('#cancelbtn').removeClass('detail_btn_active');
            $('#updatesure').addClass('detail_btn_active');
            SetService.autoSet(shopid, vm.alarm, vm.open, vm.close).$promise.then(function (data) {
                $('#exampleModal').modal('hide');
                vm.messageVisible = true;
                vm.message = "强制开机成功！";
                $('#updatesure').removeClass('detail_btn_active');
                $('#cancelbtn').addClass('detail_btn_active');
            }, function (error) {
                if (error.data) {
                    alert(error.data.msg);
                }
            });
        }

    }

    function closemotal() {
        $('#exampleModal').modal('show');
        vm.status = 2;
        vm.sure = function () {
            vm.close = true;
            vm.alarm = false;
            vm.open = false;
            $('#cancelbtn').removeClass('detail_btn_active');
            $('#updatesure').addClass('detail_btn_active');
            SetService.autoSet(shopid, vm.alarm, vm.open, vm.close).$promise.then(function (data) {
                $('#exampleModal').modal('hide');
                vm.messageVisible = true;
                vm.message = "强制关机成功！";

            }, function (error) {
                if (error.data) {
                    vm.messageVisible = true;
                    vm.message = error.data.msg;
                }
            });
        };
        $('#updatesure').removeClass('detail_btn_active');
        $('#cancelbtn').addClass('detail_btn_active');
    }
}